﻿import com.dmotiko.general.*

class ScrollClip
extends BaseClip {
	
	private var mcDrag:MovieClip;
	private var mcButtonUp:MovieClip;
	private var mcButtonDown:MovieClip;
	private var mcBar:MovieClip;
	
	private var nInitY:Number;
	private var nFinalY:Number;
	private var nHeight:Number;
	private var nPos:Number;
	private var nStep:Number;
	
	function ScrollClip(){
		super();
		nInitY = mcButtonUp._y + mcButtonUp._height;
		nFinalY = mcButtonDown._y;
		if(mcDrag) nFinalY -= mcDrag._height;
		nHeight = nFinalY - nInitY;
		nPos = 0;
		nStep = 10;
		
		initButtons();
	}
		
	public function setStep(nStep):Void {
		this.nStep = nStep;
	}
	
	public function getStep():Number{
		return this.nStep;
	}
	
	public function setPos(nPos:Number, noPropagate:Boolean){
		if(nPos < 0) nPos = 0;
		if(nPos > 100) nPos = 100;
		
		if(!mcDrag){
			this.nPos = nPos;
			if(!noPropagate) propagateEvent(new BaseClipEvent(BaseClipEvent.CHANGED, this));
			
		} else {
			var n = nPos * nHeight / 100;
			mcDrag._y = nInitY + n;
			refreshPos(noPropagate);
		}
	}
	public function getPos(){
		return this.nPos;
	}
	
	private function initButtons():Void {
		var root:MovieClip = this;
		
		if(mcDrag){		
			mcDrag._y = nInitY;
			mcDrag.onPress = function(){
				this.startDrag(this, false, root.nInitY, 0, root.nFinalY, 0);
				this.onEnterFrame = function(){
					root.refreshPos();
				}
			}
			mcDrag.onRelease = function(){
				this.stopDrag();
				delete this.onEnterFrame;
			}
			mcDrag.onReleaseOutside = function(){
				this.onRelease();
			}
		}
		if(mcButtonUp){
			mcButtonUp.onRelease = function(){
				root.setPos( root.getPos() - root.nStep);
			}
		}
		if(mcButtonDown){
			mcButtonDown.onRelease = function(){
				root.setPos( root.getPos() + root.nStep);
			}
		}
		if(mcBar && mcDrag){
			mcBar.onRelease = function(){
				if(root._ymouse <= root.nFinalY) root.mcDrag._y = root._ymouse;
				else root.mcDrag._y = root.nFinalY;
				root.refreshPos();
			}
		}
	}	
		
	private function refreshPos(noPropagate){
		var n = (mcDrag._y - nInitY) * 100 / nHeight;
		n = Math.round(n);
		if(n != getPos()){
			nPos = n;
			if(!noPropagate) propagateEvent(new BaseClipEvent(BaseClipEvent.CHANGED, this));
		}
		
	}
		
}